<?php
class PersonalProfilesController extends AppController {

	var $name = 'PersonalProfiles';
	var $helpers = array('Html', 'Form', 'CountryList');
  var $uses = array('User', 'PersonalProfile'); 

  
	function view() {
		if ($this->Session->check('Auth.User')) {
			$user_id = $this->Session->read('Auth.User.id');
			$data = $this->User->read(null, $user_id);
			if (empty($data)) {
				$this->PersonalProfile->create();
				$data = array(
				  'PersonalProfile' => array(
				    'user_id' => $user_id
				  )
				);
				$this->PersonalProfile->save($data);
        $data = $this->User->read(null, $user_id);
			}
      $this->set('user', $data);
		}
		else {
      $this->Session->setFlash(__('You have to login to view your personal profile', true));
      $this->redirect($this->referer());
		}
	}

	function edit() {
		if (!$this->Session->check('Auth.User')) {
      $this->Session->setFlash(__('You have to login to view your personal profile', true));
      $this->redirect($this->referer());
		}
    $user_id = $this->Session->read('Auth.User.id');
		if (!empty($this->data)) {
			$profileData = $this->PersonalProfile->findByUserId($user_id);
			if (!empty($profileData)) {
        $this->PersonalProfile->set('id', $profileData['PersonalProfile']['id']);
			}
			$this->PersonalProfile->set('user_id', $user_id);
			if ($this->PersonalProfile->save($this->data)) { 
				$this->Session->setFlash(__('Your profile has been saved', true));
				$this->redirect(array('action'=>'view'));
			} else {
				$this->Session->setFlash(__('Your profile could not be saved. Please, try again.', true));
			}
		}
		else {
			$this->data = $this->PersonalProfile->findByUserId($user_id);
		}
	}

}
?>